package com.x.xiaoshuo;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;

import com.baidu.mobstat.StatService;
import com.hwangjr.rxbus.RxBus;
import com.x.mvp.MVPApplication;
import com.x.mvp.injection.components.ActivityComponent;
import com.x.mvp.injection.components.FragmentComponent;
import com.x.mvp.injection.modules.ActivityModule;
import com.x.mvp.injection.modules.AppModule;
import com.x.mvp.injection.modules.FragmentModule;
import com.x.mvp.utils.ChannelUtils;
import com.x.service.di.modules.APIModule;
import com.x.xiaoshuo.di.DaggerXActivityComponent;
import com.x.xiaoshuo.di.DaggerXAppComponent;
import com.x.xiaoshuo.di.DaggerXFragmentComponent;
import com.x.xiaoshuo.di.XAppComponent;
import com.x.xiaoshuo.di.module.XiaoShuoModule;
import com.x.xiaoshuo.utils.SharedPreferencesUtil;


public class XApplication extends MVPApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        StatService.setAppKey(Constant.BAIDU_APP_ID);
        StatService.setAppChannel(this, ChannelUtils.getChannel(this), true);
        StatService.setOn(this, StatService.JAVA_EXCEPTION_LOG);
        StatService.start(this);
        RxBus.get().register(this);
        initPrefs();
    }

    protected void initPrefs() {
        SharedPreferencesUtil.init(getApplicationContext(), getPackageName() + "_preference", Context.MODE_MULTI_PROCESS);
    }

    @Override
    public void onTerminate() {
        RxBus.get().unregister(this);
        super.onTerminate();
    }

    @Override
    protected void setupComponent() {
        appComponent = DaggerXAppComponent.builder()
                .appModule(new AppModule(this))
                .aPIModule(new APIModule(this))
                .xiaoShuoModule(new XiaoShuoModule(this))
                .build();
    }

    @Override
    public FragmentComponent getFragmentComponent(Fragment fragment) {
        FragmentComponent fragmentComponent = DaggerXFragmentComponent.builder()
                .xAppComponent((XAppComponent) getAppComponent())
                .fragmentModule(new FragmentModule(fragment)).build();
        return fragmentComponent;
    }

    @Override
    public ActivityComponent getActivityComponent(Activity activity) {
        ActivityComponent activityComponent = DaggerXActivityComponent.builder()
                .xAppComponent((XAppComponent) getAppComponent())
                .activityModule(new ActivityModule(activity)).build();
        return activityComponent;
    }

}
